﻿using Core.Tools.Cache;
using Hardware.Reactor.Base.Model;
using Prism.Commands;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using UI.Application.Config.Models;
using UI.Application.Config.Services;
using UI.Application.Share.Config;

namespace UI.Application.Config.ViewModels
{
    internal class ConfigViewModel:ReactiveObject
    {
        [Reactive]
        public PidProvider PidProvider { get; set; }
        private SystemConfigManager SystemConfigManager { get; }
        private SystemConfigProvider SystemConfigProvider { get; }

        public SystemConfigModel Config { get; set; }
        [Reactive]
        public List<ConnectionMode> ConnectionModes { get; set; }

        public ICommand SubmitCommand { get; }
        private ICacheManager CacheManager { get;  }
        public ConfigViewModel(
            SystemConfigManager systemConfigManager,
            PidProvider pidProvider,
            ICacheManager cacheManager,
            SystemConfigProvider systemConfigProvider)
        {
            SystemConfigManager = systemConfigManager;
            SystemConfigProvider = systemConfigProvider;
            Config = SystemConfigManager.Config;
            PidProvider = pidProvider;
            CacheManager = cacheManager;


            ConnectionModes =new List<ConnectionMode>();
            ConnectionModes.Add(ConnectionMode.网口);
            ConnectionModes.Add(ConnectionMode.串口);


            CacheManager.Get(CacheKey.PID,out PidParameter pidParameter);
            PidProvider.Parameter = pidParameter ?? new PidParameter();


            SubmitCommand = ReactiveCommand.Create(Submit);
        }

        private void Submit()
        {
            SystemConfigManager.Save(Config);
            CacheManager.Set(CacheKey.PID, PidProvider.Parameter);
            MessageBox.Show("保存成功");
        }
    }
}
